package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.basics.rest.RestResponse;
import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.core.entity.jz.ChickenBonus;
import com.sxbbc.common.core.enums.OrderTypeEnums;
import com.sxbbc.common.core.mapper.jz.ChickenBonusMapper;
import com.sxbbc.common.core.service.IChickenBonusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.service.system.mode.ModeService;
import com.sxbbc.common.core.utils.BasePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 鸡舍分红表 服务实现类
 * </p>
 *
 * @author
 * @since 2020-04-23
 */
@Service
public class ChickenBonusServiceImpl extends ServiceImpl<ChickenBonusMapper, ChickenBonus> implements IChickenBonusService {

    @Autowired
    private ModeService modeService;
    @Autowired
    private ChickenBonusMapper chickenBonusMapper;

    @Override
    public List<ChickenBonus> selectChickenBonusByToday() {
        return chickenBonusMapper.selectChickenBonusByToday();
    }

    @Override
    public ChickenBonus countChickenBonusList(String orderCode) {
        return chickenBonusMapper.countChickenBonusList( orderCode);
    }

    @Override
    public IPage<ChickenBonus> querychickenBonusList(BasePage<ChickenBonus> page, ChickenBonus chickenBonus) {
        IPage<ChickenBonus> chickenBonusIPage = chickenBonusMapper.querychickenBonusList(page, chickenBonus);
        if (chickenBonusIPage.getSize() > 0) {
            for (ChickenBonus record : chickenBonusIPage.getRecords()) {
                record.setTypeName(OrderTypeEnums.getName(record.getType()));
            }
        }
        return chickenBonusIPage;
    }

    @Override
    public HashMap<String, Object> queryTotalFundByDate(String queryTmie) {
        String startTime = null;
        String endTime = null;
        HashMap<String, Object> hashMap = new HashMap<>();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(queryTmie)) {
            String[] times = queryTmie.split("~");
            startTime = times[0];
            endTime = times[1];
            hashMap.put("queryTmie", queryTmie);
        } else {
            startTime = DateUtil.getInstance().toDateString(new Date());
            endTime = startTime;
            hashMap.put("queryTmie", startTime + "~" + endTime);
        }
        BigDecimal totalMoney = chickenBonusMapper.queryTotalFundByDate(startTime, endTime);
        hashMap.put("totalMoney", totalMoney);
        return hashMap;
    }

    @Override
    public RestResponse handleBonusExecute(Integer id) {
        modeService.farmBonusModeSingle(id);
        return RestFactory.success("成功");
    }
}
